home *** CD-ROM | disk | FTP | other *** search
/ Utilities Professional 1-1500 / Utilities Professional 1-1500 (1994)(WPD)[!].iso / 12511500 / var1453.dms / var1453.adf / Lists / Example2.c < prev    next >
C/C++ Source or Header  |  1992-05-02  |  3KB  |  115 lines

  1. /***********************************************************/
  2. /*                                                         */
  3. /* Amiga C Encyclopedia (ACE) V3.0      Amiga C Club (ACC) */
  4. /* -------------------------------      ------------------ */
  5. /*                                                         */
  6. /* Book:    ACM System                  Amiga C Club       */
  7. /* Chapter: Lists                       Tulevagen 22       */
  8. /* File:    Example2.c                  181 41  LIDINGO    */
  9. /* Author:  Anders Bjerin               SWEDEN             */
  10. /* Date:    92-05-01                                       */
  11. /* Version: 1.00                                           */
  12. /*                                                         */
  13. /*   Copyright 1992, Anders Bjerin - Amiga C Club (ACC)    */
  14. /*                                                         */
  15. /* Registered members may use this program freely in their */
  16. /*     own commercial/noncommercial programs/articles.     */
  17. /*                                                         */
  18. /***********************************************************/
  19.  
  20. /* Demonstrates how to scan through a list from the head */
  21. /* to the tail, and the other way around.                */
  22.  
  23.  
  24. #include <exec/types.h>
  25. #include <exec/lists.h> /* This file will automatically */
  26.                         /* include the file "nodes.h".  */
  27.  
  28. /* Declare a complete node stucture: */
  29. struct ToDo
  30. {
  31.   struct Node node; /* Every node must have this. */
  32.   STRPTR Wish;      /* Our own data.              */
  33. };
  34.  
  35.  
  36. /* Declare a list structure: */
  37. struct List my_list;
  38.  
  39.  
  40. /* Node 1: */
  41. struct ToDo eat=
  42. {
  43.   { NULL, NULL, NT_UNKNOWN, 0, "Eat" },
  44.   "eat"
  45. };
  46.  
  47. /* Node 2: */
  48. struct ToDo sleep=
  49. {
  50.   { NULL, NULL, NT_UNKNOWN, 0, "Sleep" },
  51.   "rest"
  52. };
  53.  
  54. /* Node 3: */
  55. struct ToDo drink=
  56. {
  57.   { NULL, NULL, NT_UNKNOWN, 0, "Drink" },
  58.   "drink"
  59. };
  60.  
  61.  
  62. main()
  63. {
  64.   struct Node *ptr; /* Node pointer. */
  65.  
  66.   
  67.   /* Initialize our list structure: */
  68.   NewList( &my_list );
  69.  
  70.  
  71.   /* Add three nodes: */
  72.   printf( "Adding some nodes...\n" );
  73.   AddTail( &my_list, &eat );
  74.   AddTail( &my_list, &sleep );
  75.   AddTail( &my_list, &drink );
  76.  
  77.  
  78.  
  79.   /* Scan from the head to the tail: */
  80.   printf( "We will now scan from the head to the tail...\n" );
  81.  
  82.   /* Set the node pointer so it points to the first node: */
  83.   ptr = (struct Node *) my_list.lh_Head;
  84.  
  85.   /* Stay in the loop as long as there is a node after this one: */
  86.   while( ptr->ln_Succ )
  87.   {
  88.     printf( "Node %s\n", ptr->ln_Name );
  89.     ptr = ptr->ln_Succ;
  90.   }
  91.  
  92.  
  93.   /* Scan from the tail to the head: */
  94.   printf( "We will now scan from the tail to the head...\n" );
  95.  
  96.   /* Set the node pointer so it points to the last node: */
  97.   ptr = (struct Node *) my_list.lh_TailPred;
  98.  
  99.   /* Stay in the loop as long as there is a node before this one: */
  100.   while( ptr->ln_Pred )
  101.   {
  102.     printf( "Node %s\n", ptr->ln_Name );
  103.     ptr = ptr->ln_Pred;
  104.   }
  105.  
  106.  
  107.  
  108.   /* Remove all nodes from the list: */
  109.   printf( "Remove all nodes...\n" );
  110.   RemHead( &my_list, &eat );
  111.   RemHead( &my_list, &sleep );
  112.   RemHead( &my_list, &drink );
  113. }
  114.  
  115.